package com.weiyi.mashangzhu.Roomer.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.okhttp.Request;
import com.weiyi.mashangzhu.API.API;
import com.weiyi.mashangzhu.Base.BaseActivity;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Utils.LoadingDialog;
import com.weiyi.mashangzhu.Utils.SPUtils;
import com.weiyi.mashangzhu.Utils.T;
import com.weiyi.mashangzhu.Weight.PasswordEditText;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ChangeRealPwdActivity extends BaseActivity implements OnClickListener, PasswordEditText.PasswordFullListener {
    @BindView(R.id.password_edit_text)
    PasswordEditText passwordEditText;
    ImageView delete, deletekeyboard;
    TextView one, two, three, four, five, six, seven, eight, nine, zero;
    @BindView(R.id.back)
    ImageView back;
    String info;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.tabs)
    TextView tabs;
    static ChangeRealPwdActivity intence;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_real_pwd);
        ButterKnife.bind(this);
        intence = this;
        info = getIntent().getStringExtra("info");
        if (info.equals("set")) {
            //设置支付密码
            title.setText("设置交易密码");
            tabs.setText("请输入6位数字提现密码");

        } else {
            //重置支付密码
            title.setText("重置交易密码");
            tabs.setText("输入支付密码，完成验证");


        }
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        //进入界面就需要显示对话框，如果Activity没有创建完成，Activity还没完全显示出来就显示PopupWindow的话，会出现异常现象
        //所以异步显示PopupWindow
        final View mView = this.getWindow().getDecorView();
        try {
            //延时显示提示对话框，使对话框覆盖在加载数据的progressWindow上
            mView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    showPopupWindow();
                }
            }, 200);
        } catch (Exception e) {
            e.printStackTrace();
        }
        passwordEditText.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopupWindow();
            }
        });
        back.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        for (int i = 0; i < 6; i++) {
            passwordEditText.deleteLastPassword();
        }
    }

    public void showPopupWindow() {
        View contentView = LayoutInflater.from(this).inflate(R.layout.dialog_customer_keyboard, null);
        final PopupWindow popupWindow = new PopupWindow(contentView,
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setTouchable(true);
        popupWindow.setAnimationStyle(R.style.main_menu_animstyle);
        popupWindow.setBackgroundDrawable(getResources().getDrawable(
                R.drawable.shape_pop_bg));
        deletekeyboard = (ImageView) contentView.findViewById(R.id.delete_dialog);
        delete = (ImageView) contentView.findViewById(R.id.delete);
        one = (TextView) contentView.findViewById(R.id.one);
        two = (TextView) contentView.findViewById(R.id.two);
        three = (TextView) contentView.findViewById(R.id.three);
        four = (TextView) contentView.findViewById(R.id.four);
        five = (TextView) contentView.findViewById(R.id.five);
        six = (TextView) contentView.findViewById(R.id.six);
        seven = (TextView) contentView.findViewById(R.id.seven);
        eight = (TextView) contentView.findViewById(R.id.eight);
        nine = (TextView) contentView.findViewById(R.id.nine);
        zero = (TextView) contentView.findViewById(R.id.zero);

        delete.setOnClickListener(this);
        one.setOnClickListener(this);
        two.setOnClickListener(this);
        three.setOnClickListener(this);
        four.setOnClickListener(this);
        five.setOnClickListener(this);
        six.setOnClickListener(this);
        seven.setOnClickListener(this);
        eight.setOnClickListener(this);
        nine.setOnClickListener(this);
        zero.setOnClickListener(this);
        passwordEditText.setOnPasswordFullListener(this);
        // 设置好参数之后再show
        if (popupWindow.isShowing()) {

        } else {
            popupWindow.showAsDropDown(contentView);
        }

        deletekeyboard.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
    }

    @Override
    public void onClick(View v) {
        View clickView = v;
        if (clickView instanceof TextView) {
            // 如果点击的是TextView
            String number = ((TextView) clickView).getText().toString();
            if (!TextUtils.isEmpty(number)) {
                passwordEditText.addPassword(number);
            }
        } else if (clickView instanceof ImageView) {
            // 如果是图片那肯定点击的是删除
            passwordEditText.deleteLastPassword();
        }
    }

    @Override
    public void passwordFull(final String password) {
        if (info.equals("set")) {
            Intent intent = new Intent(this, ChangeRealPwdActivity2.class);
            intent.putExtra("pwd", password);
            intent.putExtra("info", info);
            startActivity(intent);
        } else {
            LoadingDialog.show(this);
            OkHttpUtils.get()
                    .addParams("token", SPUtils.get(ChangeRealPwdActivity.this, "token", "") + "")
                    .url(API.BaseApi_L + API.GetPwd)
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Request request, Exception e) {
                            LoadingDialog.dismiss(ChangeRealPwdActivity.this);
                            T.showShort(ChangeRealPwdActivity.this, "数据获取失败，请稍后再试");

                        }

                        @Override
                        public void onResponse(String response) {
                            LoadingDialog.dismiss(ChangeRealPwdActivity.this);
                            Log.e("pppppppppppp", "=" + response);
                            if (response != null) {
                                try {
                                    final JSONObject jsonObject = new JSONObject(response);
                                    if (jsonObject.getInt("status") == 0) {
                                        if (jsonObject.getString("data") == null || jsonObject.getString("data").equals("null") || jsonObject.getString("data").equals("")) {
                                            //无密码
                                            LoadingDialog.dismiss(ChangeRealPwdActivity.this);
                                            T.showShort(ChangeRealPwdActivity.this, "你还没有设置提现密码");
                                        } else {
                                            //有密码
                                            OkHttpUtils.get()
                                                    .addParams("token", SPUtils.get(ChangeRealPwdActivity.this, "token", "") + "")
                                                    .addParams("olduAccountpw", password)
                                                    .url(API.BaseApi_R + API.JudgePwd)
                                                    .build()
                                                    .execute(new StringCallback() {
                                                        @Override
                                                        public void onError(Request request, Exception e) {
                                                            LoadingDialog.dismiss(ChangeRealPwdActivity.this);
                                                            T.showShort(ChangeRealPwdActivity.this, "数据获取失败，请稍后再试");
                                                        }

                                                        @Override
                                                        public void onResponse(String response) {
                                                            Log.e("pppppppppppp", "=" + response);
                                                            LoadingDialog.dismiss(ChangeRealPwdActivity.this);
                                                            if (response != null) {
                                                                try {
                                                                    JSONObject jsonObject1 = new JSONObject(response);
                                                                    if (jsonObject1.getInt("status") == 0) {
                                                                        //原密码输入正确
                                                                        Intent intent = new Intent(ChangeRealPwdActivity.this, ChangeRealPwdActivity2.class);
                                                                        intent.putExtra("pwd", password);
                                                                        intent.putExtra("info", info);
                                                                        ChangeRealPwdActivity.this.startActivity(intent);
                                                                    } else {
                                                                        //原密码输入错误
                                                                        T.showShort(ChangeRealPwdActivity.this, "原密码输入错误");
                                                                    }
                                                                } catch (JSONException e) {
                                                                    e.printStackTrace();
                                                                }
                                                            }
                                                        }
                                                    });
//                                            showPopupWindow();
                                            if (password.equals(jsonObject.getString("data"))) {

                                            } else {

                                            }
                                        }
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }

                        }
                    });
        }

    }

    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }
}
